<template>
  <div class="new-purchase-layout">
    <crumbList :navList="navList"></crumbList>
    <div class="new-layout">
      <div class="new-content">
        <el-form :model="newBuild" :rules="newRules" ref="newBuild" label-width="110px" class="demo-ruleForm">
          <h2 class="new-ititle">1.基本信息</h2>
          <el-form-item label="城市" prop="cityName">
            <el-select v-model="newBuild.cityName" placeholder="请选择" class="w280" disabled>
              <el-option :label="item.cityName" :value="item.cityName" v-for="(item,index) in cityMsg"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
          <div class="new-oneline clearfix">
            <el-form-item label="厂商" prop="factoryName" class="mr fl">
              <el-select v-model="newBuild.factoryName" placeholder="请选择" class="w280" disabled>
                <el-option :label="item.factoryName" :value="item.factoryName" v-for="(item,index) in factoryMsg"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产品名称" prop="machineName" class="fl">
              <el-select v-model="newBuild.machineName" placeholder="请选择" class="w280" :disabled="verify">
                <el-option :label="item.productName" :value="item.productName" v-for="(item,index) in productMsg"
                           :key="index"></el-option>
                <li class="new-add-btn" @click="addData=false" v-show="addData" v-if="false">+ 新增</li>
                <li class="new-add-edit" v-show="!addData">
                  <el-form :model="addItem" ref="addItem" :rules="addRule">
                    <el-form-item prop="factoryName">
                      <el-input v-model="addItem.factoryName" placeholder="厂商名称"></el-input>
                    </el-form-item>
                    <el-form-item prop="machineName">
                      <el-input v-model="addItem.machineName" placeholder="产品名称"></el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button class="new-btn-no buleSpaceBtn" @click="resetForm('addItem')">取消</el-button>
                      <el-button class="new-btn-ok buleBtn" @click="submitForm('addItem')">完成</el-button>
                    </el-form-item>
                  </el-form>
                </li>
              </el-select>
            </el-form-item>
          </div>
          <h2 class="new-ititle">2.安装详情</h2>
          <div class="new-oneline clearfix">
            <el-form-item label="安装日期" prop="installTime" class="mr fl">
              <el-date-picker type="date" placeholder="请选择" v-model="newBuild.installTime"
                              class="w280" :disabled="verify"></el-date-picker>
            </el-form-item>
            <el-form-item label="安装人" prop="installMan" class="fl">
              <el-input v-model="newBuild.installMan" placeholder="请输入" :disabled="verify"></el-input>
            </el-form-item>
          </div>
          <div class="new-oneline clearfix">
            <el-form-item label="预计安装数量" prop="exceptInstallCount" class="mr fl">
              <el-input v-model.number="newBuild.exceptInstallCount" placeholder="请输入" class="w280"
                        :disabled="verify"></el-input>
            </el-form-item>
            <el-form-item label="负责人" prop="chargeMan" class="fl">
              <el-input v-model="newBuild.chargeMan" placeholder="请输入" :disabled="verify"></el-input>
            </el-form-item>
          </div>
          <div class="new-oneline clearfix">
            <el-form-item label="安装线路" prop="installLine" class="mr fl">
              <el-input type="textarea" v-model="newBuild.installLine" class="w280 h120"
                        placeholder="请输入,用逗号隔开" :disabled="verify"></el-input>
            </el-form-item>
            <el-form-item label="安装地点" prop="installAddress" class="fl">
              <el-input type="textarea" v-model="newBuild.installAddress" class="w280 h120"
                        placeholder="请输入，用逗号隔开" :disabled="verify"></el-input>
            </el-form-item>
          </div>
          <div class="new-prove" v-show="verify">
            <h2 class="new-ititle">3.安装核实</h2>
            <div class="new-oneline clearfix">
              <el-form-item label="实际安装数量" prop="actualInstallCount" class="mr fl">
                <el-input v-model.number="newBuild.actualInstallCount" placeholder="请输入" class="w280"></el-input>
              </el-form-item>
              <el-form-item label="交接人" prop="receiveMan" class="fl">
                <el-input v-model="newBuild.receiveMan" placeholder="填写下环节机具安装人员"></el-input>
              </el-form-item>
            </div>
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="newBuild.remark" class="w280 h120" placeholder="请输入"></el-input>
            </el-form-item>
          </div>
          <el-form-item class="new-submit">
            <el-button class="new-btn-no buleSpaceBtn" @click="resetForm">取消</el-button>
            <el-button class="new-btn-ok buleBtn" @click="submitForm('newBuild')">完成</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import crumbList from '../../common/crumbList.vue'
  import ajax from '../../../utils/ajax/index'
  import getParam from '../../../utils/getParams/getParams'
  import resetDate from '../../../utils/date/resetDate'
  import { mapState } from 'vuex'

  export default {
    data () {
      return {
        navList: [
          {name: '项目详情', path: ''},
          {name: '机具安装', path: 'install'},
          {
            name: JSON.parse(sessionStorage.getItem('projectItemData')).name,
            path: 'installList?id=' + getParam('id')
          },
          {
            name: '新增安装信息',
            path: 'newInstall?id=' + getParam('id')
          }
        ],
        newBuild: {
          cityName: JSON.parse(sessionStorage.getItem('projectItemData')).cityName,
          factoryName: JSON.parse(sessionStorage.getItem('projectItemData')).factoryName,
          machineName: '',
          installTime: '',
          installMan: '',
          exceptInstallCount: '',
          chargeMan: '',
          installLine: '',
          installAddress: '',
          actualInstallCount: '',
          receiveMan: '',
          remark: ''
        },
        newRules: {
          cityName: [
            {required: true, message: '请选择城市', trigger: 'change'}
          ],
          factoryName: [
            {required: true, message: '请选择厂商', trigger: 'change'}
          ],
          machineName: [
            {required: true, message: '请选择产品名称', trigger: 'change'}
          ],
          installMan: [
            {required: true, message: '请输入安装人', trigger: 'blur'}
          ],
          exceptInstallCount: [
            {required: true, message: '请输入数量'},
            {type: 'number', message: '必须为数字值'}
          ],
          chargeMan: [
            {required: true, message: '请输入负责人', trigger: 'blur'}
          ],
          installLine: [
            {required: true, message: '请输入安装线路', trigger: 'blur'}
          ],
          installAddress: [
            {required: true, message: '请输入安装地点', trigger: 'blur'}
          ]
        },
        addData: true,
        addItem: {
          factoryName: '',
          machineName: ''
        },
        addRule: {
          factoryName: [
            {required: true, message: '请填写厂商名称', trigger: 'blur'}
          ],
          machineName: [
            {required: true, message: '请填写产品名称', trigger: 'blur'}
          ]
        },
        verify: false
      }
    },
    computed: {
      ...mapState(['cityMsg', 'factoryMsg', 'productMsg'])
    },
    components: {crumbList},
    created () {
      let itemData = JSON.parse(sessionStorage.getItem('install'))
      if (itemData) {
        this.verify = true
        this.newBuild = itemData
        this.navList[3].name = '核实安装信息'
      }
      let itemData2 = JSON.parse(sessionStorage.getItem('installChange'))
      if (itemData2) {
        this.newBuild = itemData2
        this.navList[3].name = '修改安装信息'
      }
    },
    methods: {
      // 重置数据
      resetForm () {
        this.$router.push('/installList?id=' + getParam('id'))
        /* let itemData = JSON.parse(sessionStorage.getItem('purchase'))
        if (itemData) {
          this.newBuild = itemData
        } else {
          this.newBuild = {
            cityName: '',
            factoryName: '',
            machineName: '',
            installTime: '',
            installMan: '',
            exceptInstallCount: '',
            chargeMan: '',
            installLine: '',
            installAddress: '',
            actualInstallCount: '',
            receiveMan: '',
            remark: ''
          }
        } */
      },
      // 提交保存数据
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.newBuild.installTime === '') {
              this.$message({message: '请选择安装时间！', type: 'warning'})
            } else {
              this.saveNewData()
            }
          } else {
            this.$message({message: '请填写相关信息！', type: 'warning'})
          }
        })
      },
      saveNewData () {
        this.newBuild.installTime = resetDate(this.newBuild.installTime)
        let params = this.newBuild
        params.projectCode = getParam('id')
        let itemData = JSON.parse(sessionStorage.getItem('install'))
        if (itemData) {
          params.id = itemData.id
        }
        let itemData2 = JSON.parse(sessionStorage.getItem('installChange'))
        if (itemData2) {
          params.id = itemData2.id
        }
        let url = this.api2.install.addProjectData
        ajax.post(url, params).then((res) => {
          if (res.msg.code === '20000') {
            this.$message({message: res.msg.message, type: 'success'})
            this.$router.push('/installList?id=' + getParam('id'))
          } else {
            this.$message.error(res.msg.message)
          }
        })
      }
    }
  }
</script>
